home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Softshoe / Lisa's Portable Parts / BitArray / BitReference.h < prev    next >
Encoding:
Text File  |  2000-06-23  |  765 b   |  45 lines

  1. // BitReference.h
  2.  
  3. #ifndef BitReference_h
  4. #define BitReference_h
  5.  
  6. #ifndef Assert_h
  7. #include "Assert.h"
  8. #endif
  9.  
  10. class BitReference
  11.   {
  12.     private:
  13.         uint32& unit;
  14.         const uint32 mask;
  15.     
  16.     public:
  17.         BitReference( uint32& theUnit, uint32 bitNumber )
  18.           : unit( theUnit ),
  19.              mask( 1ul << bitNumber )
  20.           {
  21.             Assert( bitNumber < 32 );
  22.           }
  23.         
  24.         operator bool() const        { return (unit & mask) != 0; }
  25.         
  26.         void operator=( bool b )
  27.           {
  28.             if ( b )
  29.                 Set();
  30.              else
  31.                 Clear();
  32.           }
  33.  
  34.         
  35.         void Set()                        { unit |= mask; }
  36.         void Clear()                    { unit &= ~mask; }
  37.         void Change()                    { unit ^= mask; }
  38.         
  39.         void operator&=( bool b )    { if ( !b ) Clear(); }
  40.         void operator|=( bool b )    { if ( b ) Set(); }
  41.         void operator^=( bool b )    { if ( b ) Change(); }
  42.   };
  43.  
  44. #endif
  45.